package com.example.aiprogrammingassistant.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.handlers.HandlerUtil;

public class SwitchPerspectiveHandler extends AbstractHandler {
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
        String perspectiveId = "com.example.aiprogrammingassistant.perspectives.AIProgrammingAssistantPerspective";
        IWorkbench workbench = PlatformUI.getWorkbench();
		try {
			workbench.showPerspective(
			    perspectiveId,
			    window,
			    window.getActivePage().getInput()  // 传递当前页面的输入上下文
			);
		} catch (WorkbenchException e) {
		    MessageDialog.openError(
		            window.getShell(),
		            "切换视角失败",
		            "无法打开 AI 编程助手视角：" + e.getMessage()
		        );
			e.printStackTrace();
		}
        return null;
    }
}